class Admin::EntryController < Admin::BaseAdminController
  
  before_filter :load_categories, :except => [:delete, :save_draft]
  before_filter :load_tags, :only => [:create, :update, :auto_complete_tag_list]
  
  def create_draft
    if request.xml_http_request?
      @entry = Entry.new(:title => params[:entry][:title],
        :content => params[:entry][:content], 
        :tag_list => params[:entry][:tag_list],
        :draft => true)
      
      if Category.find(params[:entry][:category]).entries << @entry
        flash[:notice] = "Draft saved at #{Time.now.to_s(:db)}"
        render :update do |page|
          page.redirect_to :action => :update, :id => @entry.id
        end
      else
        error_msg = error_msg_of_model(@entry, "Can not create draft")
        render :update do |page|
          page['draft_message'].update(error_msg)
          page['draft_message'].visual_effect(:highlight, :startcolor => "00FF00", :endcolor => "#EEEEEE")
        end
      end
    end
  end

  def create
    if request.get?
      @entry = Entry.new
    else
      @entry = Entry.new(:title => params[:entry][:title],
        :content => params[:entry][:content], 
        :tag_list => params[:entry][:tag_list],
        :draft => false)
      if Category.find(params[:entry][:category]).entries << @entry
        flash[:notice] = "Post #{@entry.title} saved."
        redirect_to(:action => "list")
      else
        flash.now[:notice] = error_msg_of_model(@entry, "Can not create post")
      end
    end
  end

  def list
    if params[:category]
      @entries = Category.find(params[:category]).entries.paginate :per_page => @@PER_PAGE, 
        :page => params[:page], :order => "created_at desc"
    else
      @entries = @blog.entries.paginate :per_page => @@PER_PAGE, :page => params[:page], 
        :order => "created_at desc"
    end
  end

  def update_draft
    if request.xhr?
      @entry = Entry.find(params[:entry][:id])
      if @entry.update_attributes({:title => params[:entry][:title],
            :content => params[:entry][:content],
            :tag_list => params[:entry][:tag_list],
            :category => Category.find(params[:entry][:category]),
            :draft => true})
        render :text => "Draft saved at #{Time.now.to_s(:db)}"
      else
        render :text => error_msg_of_model(@entry, "Can not update draft.")
      end
    end
  end
  
  def update
    if request.get?
      @entry = Entry.find(params[:id])      
    else
      @entry = Entry.find(params[:entry][:id])
      if @entry.update_attributes({:title => params[:entry][:title],
            :content => params[:entry][:content],
            :tag_list => params[:entry][:tag_list],
            :category => Category.find(params[:entry][:category]),
            :draft => false})
        flash[:notice] = "Post #{@entry.title} updated."
        redirect_to(:action => "list")
      else
        flash[:notice] = error_msg_of_model(@entry, "Can not update post.")
      end
    end
  end

  def delete
    if request.post?
      entry = Entry.find(params[:id])
      if entry.destroy
        flash[:notice] = "Post #{entry.title} deleted."
        redirect_to(:action => "list")
      end
    end
  end
  
  def auto_complete_tag_list
    re = Regexp.new("^#{params[:entry][:tag_list]}", "i")
    @matched_tag_list = @tags.find_all {|i| i.name.match re}
    render :layout => false
  end
  
  private
  #  load current blog's all categories
  def load_categories
    @categories = @blog.categories
    @categories = [] if @categories.nil?
  end
  #  load current blog's all tags
  def load_tags
    @tags = @blog.entries.tag_counts
    @tags = [] if @tags.nil?
  end
  
  #  generate model error msg
  #  ##############################
  #  
  # return string containing error message
  #
  def error_msg_of_model(model, headline_msg)
    error_msg = headline_msg 
    error_msg << "<br/><ul>"
    model.errors.each_full do |msg|
      error_msg << "<li>#{msg}</li>"
    end
    error_msg << "</ul>"
  end
end
  